// pages/works/works.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    page: 1,
    jueList: [],
    tabs: [
      {
        title: '推荐'
      },
      {
        title: '前端'
      },
      {
        title: '运营'
      },
      {
        title: '后端'
      },
      {
        title: 'Android'
      }
    ],
    state: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onReady: function (options) {
    /**
     * 模糊搜索接口-暂未使用
     * https://jue.leheavengame.com/api/view/search?page=1&keyword=测试
     */
    this.getList()
  },

  onShareAppMessage: function () {
    return {
      title: '前端历程',
      path: '/pages/works/works',
      imageUrl: '/lib/images/banner1.png'
    }
  },

  getList(id) {
    wx.showLoading({
      title: '拼命加载中',                             
      mask:true
    })
    wx.request({
      url: `https://jue.leheavengame.com/api/article/getArticleList?page=${this.data.page}&tagId=${id || ''}`,
      success: (res) => {
        res.data.data.map(item => {
          item.meta.updatedAt = item.meta.updatedAt.substr(0, 10)
          item.screenshot = item.screenshot || 'https://www.qqlykm.cn/api/ACG/api.php'
          return item
        })
        this.setData({
          state: true,
          jueList: [...this.data.jueList, ...res.data.data]
        })
        wx.hideLoading()
      },
      fail: (err) => {
        this.setData({
          state: true,
          jueList: []
        })
        wx.hideLoading()
      }
    })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      page: 1,
      jueList: []
    })
    this.getList()
    wx.stopPullDownRefresh()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.setData({
      page: this.data.page + 1
    })
    this.getList()
  },
  getMore: function () {
    this.setData({
      page: this.data.page + 1
    })
    this.getList()
  },
  bindchange: function(data) {
    this.setData({
      state: false,
      page: 1,
      jueList: []
    })
    const i = data.detail.index
    switch(i) {
      case 0:
        this.getList()
        break
      case 1:
        this.getList('5e942132c8ef34100c1834b0')
        break
      case 2:
        this.getList('5fc5f181338dba15864e89c6')
        break
      case 3:
        this.getList('5e941ebcc8ef34100c1834af')
        break
      case 4:
        this.getList('5e942153c8ef34100c1834b1')
        break
    }
  }
})